//
//  TableViewCell.swift
//  myhub
//
//  Created by x on 2024/6/3.
//

import UIKit
import RxSwift
import RxCocoa
import XDesignKit

open class TableViewCell: UITableViewCell {
    public var cellDisposeBag = DisposeBag()

    open var isSelection = false
    open var selectionColor: UIColor? {
        didSet {
            setSelected(isSelection, animated: true)
        }
    }

    open lazy var containerView: View = {
        let view = View()
        view.backgroundColor = .clear
        view.cornerRadius = Dimensions.cornerRadius
        self.addSubview(view)
        view.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(horizontal: self.inset, vertical: self.inset/2))
        }
        return view
    }()

    open lazy var stackView: StackView = {
        let subviews: [View] = []
        let view = StackView(arrangedSubviews: subviews)
        view.axis = .horizontal
        view.alignment = .center
        self.containerView.addSubview(view)
        view.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(self.inset/2)
        }
        
        return view
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        makeUI()
    }
    open override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        backgroundColor = selected ? selectionColor : .clear
    }
    open func makeUI() {
        layer.masksToBounds = true
        selectionStyle = .none
        backgroundColor = .clear

        theme.selectionColor = themeService.attribute { $0.primary }
        containerView.theme.backgroundColor = themeService.attribute { $0.primary }

        updateUI()
    }

    open func updateUI() {
        setNeedsDisplay()
    }

    open func bind(to viewModel: TableViewCellViewModel) {

    }
    

}
